package controller;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import model.*;

/*****************************************
 * onBank
 * 
 * frogState for when the frog is at initial position
 * 
 * @author Stephen Gregory, Jun Wang
 *
 */

public class onBank implements frogState {
	
	@Override
	public void handleInput(userInputController c, char keyinput) {
		
		if (keyinput == KeyEvent.VK_ESCAPE){
			c.model.setActiveLog(null);
			c.model.setNumLettersTyped(0);
		}
		else if (c.model.getActiveLog() == null) {
			//if no active log, get the list of logs
			ArrayList<log> list = c.model.getLogs();
			
			//for the list of logs, compare the first letter with input
			//Note: need to make more efficient by only checking words in first
			//row
			for (int i = 0; i < list.size(); i++) {
				//checks if keyinput matches and log is in first row
				if ((c.model.getFrog().getPosY() - list.get(i).getPosY() < 110) 
						&& (list.get(i).getWord().charAt(0) == keyinput)) {
					c.model.setActiveLog(list.get(i));
					c.model.setActiveLogIndex(i);
					c.model.setNumLettersTyped(1);
				}
			}
		}
		else if (c.model.getActiveLog().getPosX()<900 && c.model.getActiveLog().getPosX()>0) {
				log activelog = c.model.getActiveLog();
				int letterTyped = c.model.getNumLettersTyped();
				
				//if the whole word is typed
				if (keyinput == activelog.getWord().charAt(letterTyped)
						&& letterTyped == activelog.getWord().length()-1){
					c.model.incCurScore();
					c.model.activeLogTyped();
					c.setState(c.onLogState);
				}
				if (keyinput == activelog.getWord().charAt(letterTyped)) {
					//increment
					letterTyped++;
					c.model.setNumLettersTyped(letterTyped);
				}
			}
		}
		
	
	@Override
	public void frogHitEdge(userInputController c) {
		//do nothing		
	}

}
